package com.cskaoyan.service;

import com.cskaoyan.bean.MarketCoupon;
import com.cskaoyan.bean.MarketCouponExample;
import com.cskaoyan.bean.MarketCouponUser;
import com.cskaoyan.bean.MarketCouponUserExample;
import com.cskaoyan.bean.marketingmanagementbean.CouponData;
import com.cskaoyan.bean.marketingmanagementbean.CouponUserData;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.wx.home.vo.CouponVo;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建日期: 2022/05/05 23:28
 *
 * @author liutao
 */
@Service
public class CouponServiceImpl implements CouponService{

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Override
    public CouponData query(BaseParam param, String name, Integer type, Integer status) {
        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);

        //查询
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type.shortValue());
        }
        if (status != null) {
            criteria.andStatusEqualTo(status.shortValue());
        }
        criteria.andDeletedEqualTo(false);
        marketCouponExample.setOrderByClause(sort + " " + order);

        // desc limit是sql语句中的关键字，作为列名需要`desc`这样写
        List<MarketCoupon> marketCouponList = marketCouponMapper.selectByExample(marketCouponExample);
        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<MarketCoupon> pageInfo = new PageInfo<>(marketCouponList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return CouponData.data(total,pages,limit,page,marketCouponList);
    }

    @Override
    public void createCoupon(MarketCoupon marketCoupon) {
        int insert = marketCouponMapper.insert(marketCoupon);
    }

    @Override
    public void couponUpdate(MarketCoupon marketCoupon) {
        int i = marketCouponMapper.updateByPrimaryKey(marketCoupon);
    }

    @Override
    public void couponDelete(MarketCoupon marketCoupon) {
        int i = marketCouponMapper.updateByPrimaryKey(marketCoupon);
    }

    @Override
    public CouponUserData listuser(BaseParam baseParam, MarketCouponUser marketCouponUser) {
        Integer page = baseParam.getPage();
        Integer limit = baseParam.getLimit();
        String sort = baseParam.getSort();
        String order = baseParam.getOrder();
        PageHelper.startPage(page, limit);

        MarketCouponUserExample example = new MarketCouponUserExample();
        example.setOrderByClause(sort + " " + order);
        MarketCouponUserExample.Criteria criteria = example.createCriteria();
        if (marketCouponUser.getCouponId() != null && !"".equals(marketCouponUser.getCouponId())) {
            criteria.andCouponIdEqualTo(marketCouponUser.getCouponId());
        }
        List<MarketCouponUser> marketCouponUserList = marketCouponUserMapper.selectByExample(example);

        PageInfo<MarketCouponUser> pageInfo = new PageInfo<>(marketCouponUserList);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        return CouponUserData.data(total, pages, limit, page, marketCouponUserList);

    }

    @Override
    public MarketCoupon couponRead(String id) {
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(Integer.valueOf(id));
        return marketCoupon;
    }

    /*
     * 创建日期：2022/05/08 13:28
     * @return * @return: List<CouponVo>
     * @author JinLu Zhou
     */
    @Override
    public List<CouponVo> queryAllBrandCoupon() {
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria criteria = marketCouponExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(marketCouponExample);
        ArrayList<CouponVo> couponVos = new ArrayList<>();
        for (MarketCoupon marketCoupon : marketCoupons) {
            CouponVo couponVo = new CouponVo();
            couponVo.set(marketCoupon);
            couponVos.add(couponVo);
        }
        return couponVos;
    }
}
